const cronDurationStr = '<input class="cron-duration-value" type="text" value="{0}">';
const cronAdvanceStr = '<input class="cron-advance-value" type="text" value="{0}">';

const cronValueStr = '<cron-expression-input' +
    ' height="34px" width="600px" color="d58512"' +
    ' required="false" hotValidate="true"' +
    ' value="{0}"' +
    ' ></cron-expression-input>';

const cronOptStr = '<button class="el-button stop-submit el-button--danger el-button--small" type="button" onclick="deleteCronItem(this)"> <i class="el-icon-delete"></i></button>';

if (String.prototype.format == null) {
    String.prototype.format = function() {
        var formatted = this;
        for (var arg in arguments) {
            formatted = formatted.replace("{" + arg + "}", arguments[arg]);
        }
        return formatted;
    };
}

function addCronItem(val) {
    var tbody = document.getElementById('cron-table-tbody'); //获取table表格对象
    var val = (val == null ? '* * * * *' : val);
    //添加行
    var tr = tbody.insertRow(tbody.children.length);
    //添加单元格
    var cell0 = tr.insertCell(0);
    cell0.innerHTML = cronDurationStr.format('0');
    var cell1 = tr.insertCell(1);
    cell1.innerHTML = cronAdvanceStr.format('0');
    var cell2 = tr.insertCell(2);
    cell2.innerHTML = cronValueStr.format(val);
    var cell3 = tr.insertCell(3);
    cell3.innerHTML = cronOptStr;
}

function deleteCronItem(elem) {
    elem.parentNode.parentNode.remove();
}

function crontaskSubmitFun(form) {
    var cron_list = document.getElementsByClassName('cronInsideInput');
    var duration_list = document.getElementsByClassName('cron-duration-value');
    var advance_list = document.getElementsByClassName('cron-advance-value');
    const sep = ';';
    var val = '';
    if (cron_list.length > 0) {
        for (var i = 0; i < cron_list.length - 1; ++i) {
            val += cron_list[i].value + sep + duration_list[i].value + sep + advance_list[i].value + '|';
        }
        val += cron_list[cron_list.length - 1].value + sep + duration_list[duration_list.length - 1].value + sep + advance_list[advance_list.length - 1].value;
    }
    document.getElementById('id_cron_task').value = val;
}